package com.yuma.builder;


class Car{
    private String brand;
    private String color;
    private double price;
    public static CarBuilder builder(){
        return new CarBuilder();
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", color='" + color + '\'' +
                ", price=" + price +
                '}';
    }

    static class CarBuilder{
        // 早就出现了
        Car car = new Car();

        public Car build(){
            return car;
        }
        public CarBuilder setBrand(String brand) {
            car.brand = brand;
            return this;
        }
        public CarBuilder setColor(String color) {
            car.color = color;
            return this;
        }
        public CarBuilder setPrice(double price) {
            car.price = price;
            return this;
        }
    }
}


public class BuilderTest {
    public static void main(String[] args) {
        /**
         * 1.Car类 CarBuilder类
         * 2.builder()静态方法
         * 3.CarBuilder类有brand,color,price属性
         * 4.Car也有这三个属性
         * 5.CarBuilder类有build()实例方法，返回Car
         */
        Car.CarBuilder builder = Car.builder();
        builder.setBrand("benz").setColor("red").setPrice(990000);
        Car car = builder.build();

        System.out.println(car);
    }
}
